package com.hyy.customer.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyy.api.domain.customer.CstmCustomerCmppConfig;
import com.hyy.api.domain.customer.bo.CstmCustomerCmppConfigBo;
import com.hyy.api.domain.customer.vo.CstmCustomerCmppConfigVo;
import com.hyy.common.core.utils.MapstructUtils;
import com.hyy.common.core.utils.StringUtils;
import com.hyy.common.mybatis.core.page.PageQuery;
import com.hyy.common.mybatis.core.page.TableDataInfo;
import com.hyy.customer.mapper.CstmCustomerCmppConfigMapper;
import com.hyy.customer.service.ICstmCustomerCmppConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 下游客户cmpp配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-19
 */
@RequiredArgsConstructor
@Service
public class CstmCustomerCmppConfigServiceImpl implements ICstmCustomerCmppConfigService {

    private final CstmCustomerCmppConfigMapper baseMapper;

    /**
     * 查询下游客户cmpp配置
     *
     * @param id 主键
     * @return 下游客户cmpp配置
     */
    @Override
    public CstmCustomerCmppConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询下游客户cmpp配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 下游客户cmpp配置分页列表
     */
    @Override
    public TableDataInfo<CstmCustomerCmppConfigVo> queryPageList(CstmCustomerCmppConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CstmCustomerCmppConfig> lqw = buildQueryWrapper(bo);
        Page<CstmCustomerCmppConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的下游客户cmpp配置列表
     *
     * @param bo 查询条件
     * @return 下游客户cmpp配置列表
     */
    @Override
    public List<CstmCustomerCmppConfigVo> queryList(CstmCustomerCmppConfigBo bo) {
        LambdaQueryWrapper<CstmCustomerCmppConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CstmCustomerCmppConfig> buildQueryWrapper(CstmCustomerCmppConfigBo bo) {
        LambdaQueryWrapper<CstmCustomerCmppConfig> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(CstmCustomerCmppConfig::getId);
        lqw.eq(bo.getCustomerId() != null, CstmCustomerCmppConfig::getCustomerId, bo.getCustomerId());
        lqw.eq(StringUtils.isNotBlank(bo.getSpCode()), CstmCustomerCmppConfig::getSpCode, bo.getSpCode());
        lqw.eq(bo.getConnections() != null, CstmCustomerCmppConfig::getConnections, bo.getConnections());
        lqw.eq(bo.getAcceptRate() != null, CstmCustomerCmppConfig::getAcceptRate, bo.getAcceptRate());
        lqw.eq(bo.getNeedProviderDest() != null, CstmCustomerCmppConfig::getNeedProviderDest, bo.getNeedProviderDest());
        return lqw;
    }

    /**
     * 新增下游客户cmpp配置
     *
     * @param bo 下游客户cmpp配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CstmCustomerCmppConfigBo bo) {
        CstmCustomerCmppConfig add = MapstructUtils.convert(bo, CstmCustomerCmppConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改下游客户cmpp配置
     *
     * @param bo 下游客户cmpp配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CstmCustomerCmppConfigBo bo) {
        CstmCustomerCmppConfig update = MapstructUtils.convert(bo, CstmCustomerCmppConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CstmCustomerCmppConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除下游客户cmpp配置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public CstmCustomerCmppConfigVo queryByCustomerId(Long customerId) {
        LambdaQueryWrapper<CstmCustomerCmppConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(CstmCustomerCmppConfig::getCustomerId, customerId);
        List<CstmCustomerCmppConfigVo> list = baseMapper.selectVoList(lqw);
        if (CollUtil.isEmpty(list))  return null;
        return list.get(0);


    }

    @Override
    public void deleteByCustomerId(Long customerId) {
        LambdaQueryWrapper<CstmCustomerCmppConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(CstmCustomerCmppConfig::getCustomerId, customerId);
        baseMapper.delete(lqw);

    }

    @Override
    public void updateByCustomerId(Long customerId, CstmCustomerCmppConfigBo cmppConfigBo) {
        LambdaQueryWrapper<CstmCustomerCmppConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(CstmCustomerCmppConfig::getCustomerId, customerId);
        CstmCustomerCmppConfig cmppConfig = MapstructUtils.convert(cmppConfigBo, CstmCustomerCmppConfig.class);
        baseMapper.update(cmppConfig, lqw);


    }
}
